import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:lazy_food/components/keepAliveWrapper/keep_alive_arapper.dart';
import 'package:lazy_food/components/group/group.dart';
import 'package:lazy_food/home/recommend/more.dart';
import 'package:lazy_food/home/recommend/top.dart';
import 'package:lazy_food/models/recommend_model.dart';
import 'package:lazy_food/models/story_model.dart';
import 'package:lazy_food/net/network.dart';
import 'package:lazy_food/util/string+.dart';
import '../../models/tab_model.dart';

class RecommendPage extends StatefulWidget {
  TabModel tab;
  RecommendPage({Key? key, required this.tab}) : super(key: key);
  @override
  State<RecommendPage> createState() => _RecommendPageState();
}

class _RecommendPageState extends State<RecommendPage> {
  RecommendModel? _model;
  EasyRefreshController _refreshController =
      EasyRefreshController(controlFinishRefresh: true);
  ScrollController _scrollController = ScrollController();
  // 防止多次加载
  bool _isLoading = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.offset >= 0) {
        _isLoading = false;
      }
    });
  }

  void _loadData() {
    if (_isLoading) {
      _refreshController.finishRefresh(IndicatorResult.success);
      return;
    }
    _isLoading = true;
    EasyLoading.show();
    Network.getHomeRecommend().then((value) {
      setState(() {
        _model = value;
      });
      EasyLoading.dismiss();
      _refreshController.finishRefresh(IndicatorResult.success);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.black,
      child: EasyRefresh.builder(
        controller: _refreshController,
        onRefresh: _loadData,
        refreshOnStart: true,
        childBuilder: (context, physics) {
          return KeepAliveWrapper(
            child: ListView.builder(
              itemCount: _model?.more.foods?.length ?? 0 + 3,
              controller: _scrollController,
              physics: physics,
              itemBuilder: (context, index) {
                if (index == 0) {
                  return TopView(
                    model: _model?.top,
                  );
                } else if (index == 1) {
                  List<StoryModel> stories = _model?.group.stories ?? [];
                  return GroupView(
                    list: stories.map((e) {
                      return GroupModel(
                          e.cover_image.url_pattern.toImage(), e.name);
                    }).toList(),
                    title: '合集',
                  );
                } else if (index == 2) {
                  return Container(
                    padding: EdgeInsets.only(
                        left: 20, right: 20, top: 10, bottom: 10),
                    height: 45,
                    child: Text(
                      '更多美味',
                      style: TextStyle(color: Colors.white, fontSize: 20),
                    ),
                  );
                } else {
                  return MoreView(
                    model: _model?.more.foods?[index - 3],
                  );
                }
              },
            ),
          );
        },
      ),
    );
  }
}
